from django.views import View
from django.shortcuts import render
from django.shortcuts import HttpResponse
from django.shortcuts import redirect
from django.urls import reverse
from django.http.response import JsonResponse
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from web.utils.tencent.cos import delete_bucket
from web import models
import json

class SettingView(View):

    def get(self, request, *args, **kwargs):
        return render(request, 'manage/setting.html')

@method_decorator(csrf_exempt,name='dispatch')
class DeleteView(View):

    def get(self, request, *args, **kwargs):
        return render(request, 'manage/project_delete.html')

    def post(self, request, *args, **kwargs):
        data_dict=json.loads(request.body)
        name=data_dict['name']
        try:
            project_obj=models.Project.objects.filter(name=name,creator=request.tracer.user).first()
            #1 删除项目
            #2 清空bucket
            delete_bucket(request.tracer.project.bucket,region=request.tracer.project.region)
            project_obj.delete()
            #3 跳回项目列表
            return JsonResponse({'code': 200, 'data': '删除成功', 'error': ''})
        except Exception as e:
            print(e)
            return JsonResponse({'code':400,'data':'','error':'删除失败'})



